package com.itheima.controller;

import com.github.pagehelper.Page;
import com.itheima.entity.PageResult;
import com.itheima.entity.Result;
import com.itheima.pojo.Blog;
import com.itheima.service.BlogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;


/**
 * @ClassName BlogController
 * @Description 博客表 前端控制器
 * @Author pengkaka
 * @Date 2020/8/4 10:39
 * @Version 1.0
 **/

@RequestMapping("/blog")
@RestController
@CrossOrigin(allowCredentials="true", allowedHeaders="*", methods={RequestMethod.GET,RequestMethod.POST}, origins="*")
public class BlogController {

    @Autowired
    private BlogService blogService;

    @PostMapping("/add")
    public Result add(@RequestBody Blog blog) {

        try {
            blogService.add(blog);
            return new Result("success", "添加成功");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new Result("error", "添加失败");
    }
    
    
    @PostMapping("/delete")
    public Result delete(@RequestBody Blog blog) {
        blogService.delete(blog);
        return new Result("success","删除成功");
    }

    @PostMapping("/deleteBatch")
    public Result deleteBatch(@RequestBody List<Blog> blogList) {
        blogService.deleteBatch(blogList);
        return new Result("success","删除成功");
    }
    
    @PostMapping("/edit")
    public Result edit(@RequestBody Blog blog) {
        blogService.edit(blog);
        return new Result("success","修改成功");
    }

    @PostMapping("/getList")
    public Result getList(@RequestBody Map<String, Object> searchMap) {
        Page<Blog> pageList = blogService.findPage(searchMap);
        com.itheima.entity.Page result = new com.itheima.entity.Page();
        result.setCurrent((Integer) searchMap.get("currentPage"));
        result.setTotal((int) pageList.getTotal());
        result.setRecords(pageList.getResult());

        return new Result("success", result);

    }
    

    @GetMapping
    public Result list() {
        List<Blog> blogList = blogService.list();
        return new Result("success",blogList);
    }


}
